package com.yituo.mjworld.core.web.servlet;

import com.yituo.mjworld.core.utils.ApplicationContextHolder;
import com.yituo.mjworld.core.utils.Constant;
import com.yituo.mjworld.core.web.core.MyHttpRequest;
import com.yituo.mjworld.core.web.core.MyHttpResponse;
import com.yituo.mjworld.mvc.entity.UserData;
import com.yituo.mjworld.mvc.service.impl.WxConfig;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.UUID;

/**
 * 二维码处理
 */
public class QrCodeServlet extends HttpServlet {


  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    requestHandler((MyHttpRequest) request, (MyHttpResponse) response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    requestHandler((MyHttpRequest) request, (MyHttpResponse) response);
  }

  /**
   * 请求处理函数
   * 接口格式:// http://ip:port/api(servlet名称)/action(要访问的action类名)/method(要访问的方法)
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void requestHandler(MyHttpRequest request, MyHttpResponse response)
      throws ServletException, IOException {
    String reqBrowser = request.getHeader("User-Agent");
    System.out.println(reqBrowser);
    String domain = Constant.getDomain();
    // 推荐人
    String referrer = request.getParameter("k");
    System.out.println("url is:" + domain + "goto/bindWx.html?ref=" + referrer + "&appId=" + WxConfig.APP_ID);
    System.out.println(reqBrowser.indexOf("MicroMessenger") != -1 && reqBrowser.indexOf("Mobile") != -1);
    if (reqBrowser.indexOf("MicroMessenger") != -1 && reqBrowser.indexOf("Mobile") != -1) {
      // 微信浏览器访问扫码页,不需要关注公众号的情况下可以获取到用户的微信信息.直接
      response.sendRedirect(domain + "goto/bindWx.html?ref=" + referrer + "&appId=" + WxConfig.APP_ID);
      return;
    } else {
      // 非微信浏览器,无法静默授权,跳注册页面.
      String id = request.getSession().getId();
      System.out.println("servlet:" + id);
      String valicode = UUID.randomUUID().toString().replace("-", "");
      UserData.data.put(id, valicode);
      System.out.println(valicode);
      // domain
      if (!StringUtils.isEmpty(referrer)) {
        // 跳转到一个前端不可见的静态页,实现用户的静默注册.
        response.sendRedirect(domain + "goto/bindPhone.html?v=" + valicode + "&ref=" + referrer);
        return;
      }
    }

  }

  /**
   * 分发Action
   *
   * @param req
   * @param res
   * @throws SecurityException
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   */
  public void dispatch(MyHttpRequest req, MyHttpResponse res) throws IOException {
    try {
      String actionName = req.getActionName();
      actionName = actionName + "Action";
      Object bean = ApplicationContextHolder.getBean(actionName);
      Class clazz = bean.getClass();
      Method method = null;
      method = clazz.getMethod(req.getMethodName(), MyHttpRequest.class, MyHttpResponse.class);
      method.invoke(bean, req, res);
    } catch (NoSuchMethodException e) {
      // 无匹配接口
      res.failedFor404("接口未找到");
    } catch (IllegalAccessException e) {
      // 访问了私有方法
      res.failedFor404("你没有权限访问该方法!");
    } catch (InvocationTargetException e) {
      res.failedFor404("服务器错误,请联系服务器管理人员进行检查.");
      // 后台接口执行错误
    }
    return;
  }
}
